package com.yanqu.road.server.cross3server.cmd.chat;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityChatMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Cross3Protocol.C3_CROSS_ACTIVITY_CHAT, desc = "跨地区活动聊天")
public class Cross3ActivityChatCmd extends CrossCommand {

    private CrossActivityProto.CrossActivityChatReqMsg reqMsg;

    private int activityId;
    private WorldMessageProto.WorldMessageTempMsg chatMsg;
    private boolean syncChannel = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossActivityProto.CrossActivityChatReqMsg.parseFrom(packet.getBytes());

        activityId = reqMsg.getActivityId();
        chatMsg = reqMsg.getMsg();
        if (reqMsg.hasSyncChannel()) {
            syncChannel = reqMsg.getSyncChannel();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if (syncChannel) {
            // 同步到别的地区
            CrossActivityProto.CrossActivityChatReqMsg.Builder builder = reqMsg.toBuilder();
            YanQuMessage message = YanQuMessageUtils.buildMessage(ChannelProtocol.N_CHANNEL_ACTIVITY_MESSAGE_TRANSFER, builder);
            MessageHelper.sendChannelServer(message);
        }

        ActivityChatMgr.activityChat(activityId, chatMsg);
    }

    @Override
    public void sendData() {

    }
}
